package kr.hkit.androidcomplete03.ch20;

import kr.hkit.androidcomplete03.R;
import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class WordListFragment extends ListFragment {
	OnWordChangedListener mHost;
	
	public interface OnWordChangedListener{
		public void onWordChanged(int index);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		
		try{
			mHost = (OnWordChangedListener)activity;
		} catch(ClassCastException e){
			throw new ClassCastException("activity must implement OnWordChanged");
		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		String[] WORD = getResources().getStringArray(R.array.word);
		setListAdapter(new ArrayAdapter<>(getActivity(),
				android.R.layout.simple_list_item_activated_1,
				WORD));
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		l.setItemChecked(position, true);
		mHost.onWordChanged(position);
	}
}
